package com.bw.liveplay.common.widegt

import android.R
import android.content.Context
import android.content.res.TypedArray
import android.graphics.Color
import android.util.AttributeSet
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.TextView
import com.blankj.utilcode.util.ConvertUtils
import com.bw.liveplay.App


/**
 * @author KK
 * @description:
 * @date :2021/11/10 15:50
 *
 */

internal class BottomButton(context: Context, attrs: AttributeSet?) :
    RelativeLayout(context, attrs), View.OnClickListener {

    private var linearLayout : LinearLayout ?=null//底部图片和文字的容器
    private var img : ImageView ?=null//底部图片
    private var contentTv : TextView ?=null//图片下的文字
    private var listener : BottomSelectListener ?=null
    private var selFlag = false
    private var nomalSize = ConvertUtils.sp2px(8f)//未选中按钮图片大小
    private var selSize = ConvertUtils.sp2px(10f)//选中按钮图片大小
    private var nomalColor = Color.BLACK//默认字体颜色
    private var selColor  = Color.RED//默认字体颜色
    private var nomalImg =0//未选中的图片
    private var selImg =0//选中的图片
    private var content =""//底部文字内容

    fun setListener (listener :BottomSelectListener): BottomButton{
       this.listener = listener
        return this
    }

    //设置要显示的文字内容
    fun setContent (content :String): BottomButton{
        this.content = content
        return this
    }

    //设置是否被选中
    fun setSelFlag (selFlag :Boolean): BottomButton{
        this.selFlag = selFlag
        refresh()
        return this
    }

    //未选中的字体大小
    fun setNomalSize (nomalSize :Int): BottomButton{
        this.nomalSize = nomalSize
        return this
    }

    //选中的字体大小
    fun setSelSize (selSize :Int): BottomButton{
        this.selSize = selSize
        return this
    }

    //未选中的字体颜色
    fun setNomalColor (nomalColor :Int): BottomButton{
        this.nomalColor = nomalColor
        return this
    }

    //选中的字体颜色
    fun setSelColor (selColor :Int): BottomButton{
        this.selColor = selColor
        return this
    }

    //未选中的图片
    fun setNomalImg (nomalImg :Int): BottomButton{
        this.nomalImg = nomalImg
        return this
    }

    //选中的图片
    fun setSelImg (selImg :Int): BottomButton{
        this.selImg = selImg
        return this
    }



    //初始化方法
   fun init() {
        linearLayout = LinearLayout(App.instance())
       var layoutParams = LayoutParams(
           ViewGroup.LayoutParams.WRAP_CONTENT,
           ViewGroup.LayoutParams.WRAP_CONTENT,
       )
        layoutParams.addRule(CENTER_IN_PARENT)//布局居中
        linearLayout!!.layoutParams = layoutParams
        //设置排列方向
        linearLayout!!.orientation = LinearLayout.VERTICAL
        img = ImageView(App.instance())
        var imglp = ViewGroup.LayoutParams(ConvertUtils.dp2px(25f),ConvertUtils.dp2px(25f))
        img!!.layoutParams = imglp
        linearLayout!!.addView(img)
        contentTv = TextView(App.instance())
        var contenlp = LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT
        )
        contentTv!!.layoutParams = contenlp
        linearLayout!!.addView(contentTv)
        addView(linearLayout)
        setOnClickListener(this)

    }



    override fun onClick(v: View?) {
        selFlag = true;
        refresh()
        if(listener!=null){
            listener!!.onSelect(id)
        }
    }

    //刷新UI
    fun refresh(){
        var image = nomalImg
        var tvcolor = nomalColor
        var tvsize = nomalSize
        if(selFlag){
            image = selImg
            tvcolor = selColor
            tvsize = selSize
        }
        img!!.setImageResource(image!!)
        contentTv!!.setTextColor(tvcolor)
        contentTv!!.setTextSize(tvsize.toFloat())
        contentTv!!.setText(content)


    }




    //点击选择回调监听
    interface BottomSelectListener{
        fun onSelect(id : Int)
    }
    init {
        val typedArray: TypedArray = context.obtainStyledAttributes(attrs, com.bw.liveplay.R.styleable.bottom_arrs)
        selFlag = typedArray.getBoolean(com.bw.liveplay.R.styleable.bottom_arrs_selflag, false)
        typedArray.recycle()
        init()
    }


}